package com.huibao.hulibao.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.apkfuns.logutils.LogUtils;
import com.huibao.hulibao.R;
import com.huibao.hulibao.adapter.BindClientLvAdapter;
import com.huibao.hulibao.adapter.MsgLvAdapter;
import com.huibao.hulibao.constant.GlobalConstants;
import com.huibao.hulibao.entity.HuLiBindMomEntity;
import com.huibao.hulibao.entity.LoginEntity;
import com.huibao.hulibao.entity.MessageEntity;
import com.huibao.hulibao.utils.DialogUtils;
import com.huibao.hulibao.utils.PreUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lizhaoyong on 2018/5/11.
 */
public class MyBindClientActivity extends BaseActivity {
    @BindView(R.id.lv_my_bind_client)
    ListView lv_my_bind_client;
    @BindView(R.id.iv_back)
    ImageView iv_back;

    // 网络请求加载对话框
    private Dialog myDialog;


    private LoginEntity le;


    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_bind_client);
        ButterKnife.bind(this);
        le = (LoginEntity) PreUtils.getObject(getApplicationContext(), "LoginEntity");
        getBindClient();


    }

    public void showDialog() {
        myDialog = DialogUtils.createLoadingDialog(MyBindClientActivity.this, getResources().getString(R.string.jiazaizhong));
    }

    public void closeDialog() {
        DialogUtils.closeDialog(myDialog);
    }

    // 获取绑定客户接口
    public void getBindClient() {
        if (le == null) {
            return;
        }
        RequestParams params = new RequestParams(GlobalConstants.URL);
        params.setCacheMaxAge(0);
        showDialog();
//        JSONObject object = new JSONObject();
//        try {
//            object.put("type", type);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        params.addBodyParameter("q", le.Message.Session + GlobalConstants.URL_BIND_MOM + "-");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                    // LogUtils.d("result = " + jsonObject.toString());
                    HuLiBindMomEntity hme = GlobalConstants.gson.fromJson(jsonObject.toString(), HuLiBindMomEntity.class);
                    lv_my_bind_client.setAdapter(new BindClientLvAdapter(hme.Message, getApplicationContext()));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                closeDialog();
            }

            @Override
            public void onFinished() {
                closeDialog();
            }
        });


    }

}
